package com.sunwayworld.basemodule.rm.person.service.impl;

import com.sunwayworld.basemodule.rm.person.bean.LimsPersonAbilityBean;
import com.sunwayworld.basemodule.rm.person.bean.LimsPersonCertificateBean;
import com.sunwayworld.basemodule.rm.person.service.LimsPersonAbilityService;
import com.sunwayworld.basemodule.rm.person.service.LimsPersonCertificateService;
import com.sunwayworld.framework.audit.aunnotation.Audit;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.support.PersistableMetadataHelper;
import com.sunwayworld.module.sys.metadata.service.CoreTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.rm.person.dao.LimsPersonDao;
import com.sunwayworld.basemodule.rm.person.bean.LimsPersonBean;
import com.sunwayworld.basemodule.rm.person.service.LimsPersonService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsPersonServiceImpl implements LimsPersonService {

    @Autowired
    private LimsPersonDao limsPersonDao;

    @Autowired
    private LimsPersonCertificateService limsPersonCertificateService;

    @Autowired
    private LimsPersonAbilityService limsPersonAbilityService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsPersonDao getDao() {
        return limsPersonDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsPersonBean limsPerson = jsonWrapper.parseUnique(LimsPersonBean.class);
        limsPerson.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsPerson);
        return limsPerson.getId();
    }

    @Transactional
    @AuditTrailEntry(AuditTrailType.DELETE)
    @Audit("AUDIT.DELETE")
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsPersonBean> deleteList = wrapper.parse(this.getDao().getType());
        if (!deleteList.isEmpty()) {

            List<Long> ids = deleteList.stream().map(LimsPersonBean::getId).collect(Collectors.toList());
            List<LimsPersonCertificateBean> certificateBeans = limsPersonCertificateService.selectListByFilter(SearchFilter.instance().match("personId", ids).filter(MatchPattern.OR));
            List<LimsPersonAbilityBean> abilityBeans = limsPersonAbilityService.selectListByFilter(SearchFilter.instance().match("personId", ids).filter(MatchPattern.OR));
            if (!certificateBeans.isEmpty()) {
                limsPersonCertificateService.getDao().deleteBy(certificateBeans, "id");
            }
            if (!abilityBeans.isEmpty()) {
                limsPersonAbilityService.getDao().deleteBy(abilityBeans, "id");
            }

            Class<LimsPersonBean> itemType = this.getDao().getType();
            CoreTableService tableService = (CoreTableService) ApplicationContextHelper.getBean(CoreTableService.class);
            deleteList.forEach((t) -> {
                tableService.deleteCascade(PersistableMetadataHelper.getTableName(itemType), (Serializable) t.getId());
            });
        }
    }
}
